package part2;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Lock;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class WarGamePanel extends JPanel{
	WarGame w;
	GameThread g;
	WarMouseListner m;
	Semaphore humanFlag;
	
	public WarGamePanel(Agent player1, Agent player2, String file){
		w = new WarGame( player1,  player2, file);
		g = new GameThread();
		m = new WarMouseListner();
		addMouseListener(m);
		setVisible(true);
		humanFlag = new Semaphore(1);
		try {
			humanFlag.acquire();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void start(){
		g.run();
	}
	
	public void paint(Graphics g){
		String[][] board = w.b.getString();
		for(int i=0; i< board.length;i++){
			for(int j=0; j< board[i].length;j++){
				String[] vals = board[i][j].split("/");
				if(vals[0].equals("0"))
					g.setColor(Color.GRAY);
				if(vals[0].equals("1"))
					g.setColor(Color.GREEN);
				if(vals[0].equals("2"))
					g.setColor(Color.BLUE);
				g.fillRect((this.getWidth()/(board.length))*i, (this.getHeight()/(board.length))*j, this.getWidth()/(board.length), this.getHeight()/(board.length));
				g.setColor(Color.BLACK);
				g.drawRect((this.getWidth()/(board.length))*i, (this.getHeight()/(board.length))*j, this.getWidth()/(board.length), this.getHeight()/(board.length));
				g.drawString(vals[1], (this.getWidth()/(board.length))*i+(this.getWidth()/(board.length))/2, (this.getHeight()/(board.length))*j + (this.getHeight()/(board.length))/2);
			}
		}
		
	}
	public class WarMouseListner implements MouseListener{

		@Override
		public void mouseClicked(MouseEvent arg0) {
			if(w.isP1turn && w.player1 instanceof HumanAgent){
				((HumanAgent)w.player1).executeMove(w.b, arg0.getX()/(getWidth()/(w.b.boardColors.length)), arg0.getY()/(getHeight()/(w.b.boardColors[0].length)));
				w.isP1turn = !w.isP1turn;
				humanFlag.release();
			}
			
			if(!w.isP1turn && w.player2 instanceof HumanAgent){
				((HumanAgent)w.player2).executeMove(w.b, arg0.getX()/(getWidth()/(w.b.boardColors.length)), arg0.getY()/(getHeight()/(w.b.boardColors[0].length)));
				w.isP1turn = !w.isP1turn;
				humanFlag.release();
			}
			
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		
	}
	public class GameThread implements Runnable{
		@Override
		public void run() {
			while(!w.b.isFull()){
				if(w.isP1turn && w.player1 instanceof HumanAgent){
					try {
						humanFlag.acquire();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				else if(!w.isP1turn && w.player2 instanceof HumanAgent){
					try {
						humanFlag.acquire();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				else{
					w.runMove();
				}
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				repaint();
			}
			System.out.println("player 1 expanded: "+ w.player1.nodesExpanded +", player 2 expanded: "+w.player2.nodesExpanded);
			System.out.println("player 1 time: "+ w.player1.timeWorking +", player 2 time: "+w.player2.timeWorking);
			System.out.println("player 1 avg expanded: "+ w.player1.nodesExpanded/18 +", player 2 avg expanded: "+w.player2.nodesExpanded/18);
			System.out.println("player 1 avg time: "+ w.player1.timeWorking/18 +", player 2 avg time: "+w.player2.timeWorking/18);
		}
	}
}
